const {ElMessage} = require("element-plus");
export default {
	name: "User",
	data() {
		return {
			//加载
			loading: true,
			pageNum: 1,
			pageSize: 10,
			total: 0,
			//小弹窗状态
			dialogVisible: false,
			//弹窗是否为更新状态
			judge: false,
			tableData: [],
			searchPARAM: {
				id: '',
				username: '',
				password: '',
				name: '',
				openId: '',
				roleId: '',
				status:'',
				createTime:''
			},
			form: {
				id: '',
				username: '',
				password: '',
				name: '',
				roleId: '',
				status:'',
			},

			rules: {
				username: [{
					required: true,
					message: "请输入用户名",
					trigger: "blur"
				}, ],
				role: [{
					required: true,
					message: "请选择角色权限",
					trigger: "change"
				}, ]
			}

		}
	},

	created() {
		this.load();
		this.loading = true;
		setTimeout(() => {
			//设置延迟执行
			this.loading = false;
		}, 500);
	},
	methods: {
		load() {
			
			console.log(this.pageNum)
			console.log(this.pageSize)

			this.$http.post("/user/page", {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				searchPARAM: this.searchPARAM
			}).then((res) => {
				this.tableData = res.data.data.records;
				this.total = res.data.data.total;
				this.loading = false;
			});


		},

		reset() {
			this.searchPARAM = {
				id: '',
				username: '',
				password: '',
				telphone: '',
				mail: '',
				role: ''
			}
			this.load();
		},


		add() {
			
			this.dialogVisible = true;
			this.judge = false;
		},

		cancel() {
			this.$refs.form.resetFields();
			this.dialogVisible = false;
		},

		save() {
			this.$refs.form.validate((valid) => {
				if (valid) {
					if (this.judge === false) {
						this.$http.post("/user/add", this.form)
							.then((res) => {
								if (res.data.code === '200') {
									ElMessage({
										message: "添加成功",
										type: 'success',
									})
								} else {
									ElMessage.error(res.data.msg)
								}
								this.$refs.form.resetFields();
								this.dialogVisible = false;
								this.load();
							});
					} else {
						//修改
						this.$http.put("/user/update", this.form).then((res) => {
							if (res.data.code === "200") {
								ElMessage({
									message: "修改成功",
									type: "success",
								});
								this.$refs.form.resetFields();
								this.dialogVisible = false;
								this.load();
							} else {
								ElMessage({
									message: res.data.msg,
									type: "error",
								});
							}
						});
					}
				}
			})

		},


		handleSizeChange(pageSize) {
			//改变每页个数
			this.pageSize = pageSize;
			this.load();
		},
		handleCurrentChange(pageNum) {
			//改变页码
			this.currentPage = pageNum;
			this.load();
		},

		async handleDelete(id) {
			//删除
			this.$http.delete("/user/delete/" + id).then((res) => {
				if (res.data.code === "200") {
					ElMessage({
						message: "删除成功",
						type: "success",
					});
					this.load();
				} else {
					ElMessage.error(res.data.msg)
				}
			});
		},

		handleEdit(row) {
			//修改
			//判断操作类型
			this.judge = true;
			this.dialogVisible = true;
			this.$nextTick(() => {
				this.$refs.form.resetFields();
				this.form = JSON.parse(JSON.stringify(row));

			});
		},
		
		handleChangePassword(row){
			console.log(row);
			this.$refs.form.resetFields();
			this.form = JSON.parse(JSON.stringify(row));
			this.form.password="123456"
			this.$http.put("/user/update", this.form).then((res) => {
				if (res.data.code === "200") {
					ElMessage({
						message: "重置成功,密码为123456",
						type: "success",
					});
					this.$refs.form.resetFields();
				} else {
					ElMessage({
						message: res.data.msg,
						type: "error",
					});
				}
			});
		}


	},


}
